package top.jiaman.javaweb.servlet.user;

import com.mysql.cj.util.StringUtils;
import top.jiaman.javaweb.dao.UserDAOImpl;
import top.jiaman.javaweb.dao.UserDao;
import top.jiaman.javaweb.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Jiaman
 * @Date 2020/11/28 8:38
 * @Desc
 */

@WebServlet(urlPatterns = "/user/update")
public class UserUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        UserDAOImpl uus = UserDAOImpl.getInstance();
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String address = req.getParameter("address");


        User user = uus.getUserById(id);
        if ( !StringUtils.isNullOrEmpty(name)) {
            user.setName(name);
            user.setPassword(password);
            user.setAddress(address);
        }
        int rows = uus.update(user);


        resp.setContentType("text/html");
        resp.sendRedirect("/update_status.jsp?s=" + rows);
    }
}
